﻿using System;
using System.Reflection;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoney.Data;

using FCNS;
using FCNS.Controls.Calendars;

namespace FCNS.KeepMoney.UI
{
    public partial class GtdViewMac
    {
        public void InitTasks()
        {
            tasks.Clear();
            foreach (DataRow row in KMData.GetGtdTasks(firstDate, lastDate,Program.AppConfig.App.ShowOverdueData))
            {
                if (!gtdGroups.ContainsKey((int)row["GtdGroupId"]))
                {
                    if (selectedTask != null)//
                        if (selectedTask.Row == row)
                            selectedTask = null;

                    continue;
                }


                GtdTask task = new GtdTask((GtdData)gtdGroups[(int)row["GtdGroupId"]]);
                //task.Title = (string)row["Name"];
                //task.Note = (string)row["Note"];
                //task.StartTime = (DateTime)row["StartDateTime"];
                //task.EndTime = (DateTime)row["EndDateTime"];
                //task.GtdGroup = (GtdData)gtdGroups[(int)row["GtdGroupId"]];
                //task.Finish = (bool)row["Finish"];
                task.Row = row;
                task.StartY = getPosHours(task.StartTime.Hour, task.StartTime.Minute);
                task.EndY = getPosHours(task.EndTime.Hour, task.EndTime.Minute);
                tasks.Add(task);
            }
        }

        public void ModifiGtdGroups(GtdData g, bool add)
        {
            if (add)
                gtdGroups[g.Id] = g;
            else
                gtdGroups.Remove(g.Id);
        }

        private void MoveTask()
        {
            DateTime s = selectedTask.StartTime + lastestMouse.duration;
            DateTime e = selectedTask.EndTime + lastestMouse.duration;
            if (s.Hour < configGtd.StartHour || e.Hour > configGtd.EndHour)
                return;
            if (e.Hour == configGtd.EndHour&&e.Minute != 0)
                return;

            lastestMouse.duration = lastestMouse.mouseUp - lastestMouse.mouseDown;
            selectedTask.StartTime = s;
            selectedTask.EndTime = e;
            lastestMouse.mouseDown = lastestMouse.mouseUp;
        }

        private void ReSizeTask(DateTime lastestMouseUp)
        {
            if (lastestMouseUp.Hour < configGtd.StartHour || lastestMouseUp.Hour > configGtd.EndHour)
                return;

            if (lastestMouseUp.Minute != 0)
                return;

            if (lastestMouseUp >selectedTask.StartTime)
            {
                selectedTask.EndTime = lastestMouse.mouseUp;
                selectedTask.StartTime = lastestMouse.mouseDown - lastestMouse.duration;
            }
            else
            {
                //if (lastestMouse.mouseUp < selectedTask.StartTime)
                //{
                    selectedTask.StartTime = lastestMouse.mouseUp;
                    selectedTask.EndTime = selectedTask.EndTime;
                //}
                //else
                //{
                //    selectedTask.StartTime = lastestMouseUp;
                //}
            }
        }

        private void NewTask()
        {
            Debug.Assert(selectedTask == null);
            Debug.Assert(selectedData != null);

            this.Cursor = System.Windows.Forms.Cursors.HSplit;
            lastestMouse.operationMouse = OperationMouse.Resize;

            GtdTask gt = new GtdTask(selectedData);
            gt.StartTime = lastestMouse.mouseDown;
            gt.EndTime = lastestMouse.mouseDown;//要添加新数据到数据库
            gt.GtdGroup = selectedData;

            KMData.NewGtdEntry(gt);
            tasks.Add(gt);
            selectedTask = gt;
            Program.SaveKMD = true;
            //ChangeTask();
        }

        public void RemoveTask(GtdTask task)
        {
            KMData.DeleteGtdEntry(task);
            tasks.Remove(task);
            selectedTask = null;
            UpdateGtdView(false);
            Program.SaveKMD = true;
        }

        public List<GtdTask> Tasks
        {
            get { return tasks; }
        }

        /// <summary>
        /// 激活已选择的任务编辑框的编辑状态
        /// </summary>
        private void ChangeTask()
        {
            textBox1.Text = selectedTask.Title;
            textBox1.SelectAll();
            int day = 0;
            while (firstDate.AddDays(day).DayOfWeek != selectedTask.StartTime.DayOfWeek)
                day++;

            textBox1.Left = columnWidthPlusX[day];
            textBox1.Width = columnWidthPlusX[day + 1] - columnWidthPlusX[day] + 1;
            textBox1.Top = getPosHours(selectedTask.StartTime.Hour, selectedTask.StartTime.Minute) + 15;
            if (selectedTask.StartTime.DayOfWeek != selectedTask.EndTime.DayOfWeek)
                textBox1.Height = getPosHours(23, 59) - textBox1.Top;
            else
                textBox1.Height = getPosHours(selectedTask.EndTime.Hour, selectedTask.EndTime.Minute) - textBox1.Top;

            if (textBox1.Height > 30)
                textBox1.Height = textBox1.Height - 15;
            else
            {
                if (textBox1.Height < getPosHours(0, 30) - titleHeight)
                {
                    textBox1.Height = 15;
                    textBox1.Top -= 15;
                }
            }
            textBox1.BackColor = Color.FromArgb(255, selectedTask.GtdGroup.ColorBgUnselected.R,
                selectedTask.GtdGroup.ColorBgUnselected.G, selectedTask.GtdGroup.ColorBgUnselected.B);
            textBox1.ForeColor = selectedTask.GtdGroup.ColorBorder;
            textBox1.Visible = true;
            textBox1.Focus();

            /*			Rectangle cloneRect = new Rectangle(textBox1.Left, textBox1.Top + scrollValue, textBox1.Width, textBox1.Height);
            Bitmap cloneBitmap = _offBmp.Clone(cloneRect, _offBmp.PixelFormat);
            textBox1.BackgroundImage = cloneBitmap;
            Graphics g = textBox1.CreateGraphics();
            g.DrawImage(cloneBitmap, 0, scrollValue);
        Graphics drawingSurface = Graphics.FromHwnd(Handle);
            drawingSurface.DrawImage(cloneBitmap, 0, 0);
            drawingSurface.Dispose();*/
        }

    }
}
